package graduate.wangyaqi.Exam;

import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;

import javax.swing.text.html.Option;
import java.util.Optional;

public interface ExamRepository extends CrudRepository<Exam,Long> {
    Exam findExamById(long id);

    Exam findExamByStuIDAndTaotiID(long stuID,long taotiID);

    Exam findExamByStuIDAndQuestionIDAndTaotiIDAndType(long stuId,long questionId,long taotiId, String type);

    Iterable<Exam> findAllByStuIDAndTaotiIDOrderByIdDesc(Long stuId,Long taotaoId);

    Exam findFirstByStuIDAndTaotiIDOrderByIdDesc(Long stuId, Long taotaoId);

    int countAllByStuIDAndTaotiID(Long stuId,Long taotaoId);

    @Query(value = "SELECT count(a.id) FROM exam a left join only_questions b on a.questionid = b.id where a.type='Only' and a.answer1 <> b.answer and a.stuid=?1", nativeQuery = true)
    int countByStuIDNot(long  stuID);

    @Query(value = "SELECT count(a.id) FROM exam a left join only_questions b on a.questionid = b.id where a.type='Only' and a.answer1 = b.answer and a.stuid=?1", nativeQuery = true)
    int countByStuID(long  stuID);
}
